#include "generegionviewermenusidebar.h"


SideBarPixmap::SideBarPixmap(QString f, QString h, float w, QGraphicsWidget *parent) :
    QGraphicsProxyWidget(parent)
{
    width = w;
    pixmap = new QPixmap(f);
    highlightedPixmap = new QPixmap(h);
    label = new QLabel();
    label->setStyleSheet( "QLabel { background-color : transparent; }");
    label->setPixmap(pixmap->scaledToWidth(width*.85, Qt::SmoothTransformation) );//TO DO: fix this- may not rescale properly... tried to get contents margins to fix, but this doesn't work

    setWidget(label);

    setAcceptHoverEvents(true);
    //setAcceptedMouseButtons();
}

void SideBarPixmap::hoverEnterEvent(QGraphicsSceneHoverEvent *event)
{
    label->setPixmap(highlightedPixmap->scaledToWidth(width*.85, Qt::SmoothTransformation));
}

void SideBarPixmap::hoverLeaveEvent(QGraphicsSceneHoverEvent *event)
{
    label->setPixmap(pixmap->scaledToWidth(width*.85, Qt::SmoothTransformation) );//TO DO: fix this- may not rescale properly... tried to get contents margins to fix, but this doesn't work
}

void SideBarPixmap::mousePressEvent(QGraphicsSceneMouseEvent *event)
{
    emit( clicked() );
}

float SideBarPixmap::yPos()
{
    return 0.0;//mapToParent( 0.0, label->pos().y() ).y();
}

float SideBarPixmap::height()
{
    return label->geometry().height();
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////PROJECT//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ProjectMenu::ProjectMenu(GeneRegionViewerMenuSideBar *parent) :
    QGraphicsWidget(parent)
{
    width = 300;
    height = 300;

    layout = new QGraphicsLinearLayout;
    layout->setOrientation(Qt::Vertical);
    layout->setMaximumWidth(width);

    saveLayout = new QGraphicsLinearLayout;
    saveLayout->setOrientation(Qt::Horizontal);
    saveLayout->setMaximumWidth(width);

    saveLabel = new QLabel;
    saveLabel->setText("Save project: ");
    saveLabel->setStyleSheet( " background-color: rgba(255, 255, 255, 0); color : #cccccc; font: 18pt \"Helvetica\"; ");
    saveLabelProxy = new QGraphicsProxyWidget(this);
    saveLabelProxy->setWidget(saveLabel);
    //saveLayout->addItem(saveLabelProxy);
    layout->addItem(saveLabelProxy);

    saveLineEdit = new QLineEdit;
    QDate date = QDate::currentDate();
    saveLineEdit->setText(QString(QDir::homePath() + "/project" + date.toString("ddMMyyyy") ));
    saveLineEdit->setStyleSheet("background-color: black; color: white; border: 2px solid gray;");
    saveLineEdit->setMinimumWidth(.80*width);
    saveLineEditProxy = new QGraphicsProxyWidget(this);
    saveLineEditProxy->setWidget(saveLineEdit);
    //saveLayout->addItem(saveLineEditProxy);
    layout->addItem(saveLineEditProxy);

    //layout->addItem(saveLayout);

    choose = new SideBarPixmap(":images/resources/choose.png", ":images/resources/chooseHighlight.png", 100, this );
    layout->addItem(choose);

    setLayout(layout);
}

QRectF ProjectMenu::boundingRect() const
{
    return QRectF( 0, 0, width, height);
}

QPainterPath ProjectMenu::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void ProjectMenu::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor backgroundColor;
    backgroundColor.setRgb(0.0, 0.0, 0.0, 255.0);//60.0, 60.0, 60.0, 255.0);
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    QRectF bRect = boundingRect();
    bRect.adjust(0, 1, 0, -1);
    painter->drawRoundedRect(bRect, width*.05, height*.05);

}

void ProjectMenu::setWidth(float newWidth)
{
    width = newWidth;
}

void ProjectMenu::setHeight(float newHeight)
{
    height = newHeight;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////SORT//////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

SortMenu::SortMenu(GeneRegionViewerMenuSideBar *parent) :
    QGraphicsWidget(parent)
{
    layout = new QGraphicsLinearLayout;
    layout->setOrientation(Qt::Vertical);
    layout->setMaximumWidth(300);

    contigLengthSortLayout = new QGraphicsLinearLayout;
    contigLengthSortLayout->setOrientation(Qt::Horizontal);
    contigLengthSortLayout->setMaximumWidth(300);
    layout->addItem(contigLengthSortLayout);

    longToShort  = new SideBarPixmap( ":images/resources/longToShort.png", ":images/resources/longToShortHighlighted.png", 150, this);
    contigLengthSortLayout->addItem(longToShort);
    connect( longToShort, SIGNAL(clicked()), parent, SIGNAL(sortByContigLengthLongToShortSignal()) );

    shortToLong = new SideBarPixmap( ":images/resources/shortToLong.png", ":images/resources/shortToLongHighlighted.png", 150, this);
    contigLengthSortLayout->addItem(shortToLong);
    connect( shortToLong, SIGNAL(clicked()), parent, SIGNAL(sortByContigLengthShortToLongSignal()) );

    restoreOriginalOrder = new SideBarPixmap( ":images/resources/restoreOriginalOrder.png", ":images/resources/restoreOriginalOrderHighlighted.png", 200, this);
    layout->addItem(restoreOriginalOrder);
    //TO DO- connect fn

//    QSpacerItem s(0, 10);
//    QGraphicsProxyWidget p(this);
//    p.setWidget(&s);
//    layout->addItem(&p);

    //spacer = new QSpacerItem(300, 20);
//    spacerProxy = new QGraphicsProxyWidget(this);
//    spacerProxy->setWidget(spacer);
    //layout->addItem(spacer);//Proxy);

    layout->setItemSpacing(1, 30);

    sortByLayout = new QGraphicsLinearLayout;
    sortByLayout->setOrientation(Qt::Horizontal);
    sortByLayout->setMaximumWidth(300);

    sortByText = new QLabel;
    sortByText->setText("Bring to top:");
    sortByText->setStyleSheet( " background-color: rgba(255, 255, 255, 0); color : #cccccc; font: 18pt \"Helvetica\"; ");
    sortByTextProxy = new QGraphicsProxyWidget(this);
    sortByTextProxy->setWidget(sortByText);
    sortByLayout->addItem(sortByTextProxy);

    userEntry = new QLineEdit;
    userEntry->setText(">");
    userEntry->setStyleSheet("background-color: black; color: white; border: 2px solid gray;");

    userEntryProxy = new QGraphicsProxyWidget(this);
    userEntryProxy->setWidget(userEntry);
    sortByLayout->addItem(userEntryProxy);

    layout->addItem(sortByLayout);

    typeAndGoLayout = new QGraphicsLinearLayout;
    typeAndGoLayout->setOrientation(Qt::Horizontal);
    typeAndGoLayout->setMaximumWidth(300);

    sortType = new QComboBox;
    sortType->addItem("Product");//"ClusterId");
    sortType->addItem("SeqId");
    sortType->addItem("ClusterId");//"Product");
    //sortType->setStyleSheet("background-color: gray; border: 2px solid gray; color: white");
    sortTypeProxy = new QGraphicsProxyWidget(this);
    sortTypeProxy->setWidget(sortType);
    sortTypeProxy->setZValue(100.0);
    //sortByLayout->addItem(sortTypeProxy);
    //layout->addItem(sortTypeProxy);
    typeAndGoLayout->addItem(sortTypeProxy);

    layout->addItem(typeAndGoLayout);

    goSideBarPixmap = new SideBarPixmap( ":images/resources/go.png", ":images/resources/goHighlighted.png", 100, this);
    layout->addItem(goSideBarPixmap);
    connect( goSideBarPixmap, SIGNAL(clicked()), this, SLOT(sortByElementClicked()) );
    connect( this, SIGNAL(sortByElement(QString, QString)), parent, SIGNAL(sortByEntrySignal(QString,QString)) );

    //connect( this, SIGNAL(sortByEntrySignal(QString, QString)), parent, SIGNAL(sortByEntrySignal(QString,QString)) );
    //typeAndGoLayout->addItem(goSideBarPixmap);

    setLayout(layout);
}

void SortMenu::sortByElementClicked()
{
    if( userEntry->text().compare(">") != 0 )//if user has entered sth
    {
        if( userEntry->text().startsWith('>') )
            emit( sortByElement(userEntry->text().remove(0,1), sortType->currentText() )  );
        else
            emit( sortByElement(userEntry->text(), sortType->currentText() )  );
    }
}

QRectF SortMenu::boundingRect() const
{
    return QRectF( 0, 0, width, height);
}

QPainterPath SortMenu::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void SortMenu::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor backgroundColor;
    backgroundColor.setRgb(0.0, 0.0, 0.0, 255.0);//60.0, 60.0, 60.0, 255.0);
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    QRectF bRect = boundingRect();
    bRect.adjust(0, 1, 0, -1);
    painter->drawRoundedRect(bRect, width*.05, height*.05);

}

void SortMenu::setWidth(float newWidth)
{
    width = newWidth;
}

void SortMenu::setHeight(float newHeight)
{
    height = newHeight;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////GENE REGION VIEWER MENU SIDE BAR///////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////

GeneRegionViewerMenuSideBar::GeneRegionViewerMenuSideBar( MultiGeneRegionVisWidget *parent) :
    QGraphicsWidget(parent)
{
    if( DISPLAY_MODE == DESKTOP_MODE )
    {
        width = parent->boundingRect().width()*.1;
        height = parent->boundingRect().height();
    }
    else // wall mode
    {
        width = parent->boundingRect().width()*.05;
        height = parent->boundingRect().height();
    }

    setAcceptHoverEvents(true);
    setZValue(1000.0);

    qDebug() << "creating menu bar " << endl;
    layout = new QGraphicsLinearLayout;
    layout->setOrientation(Qt::Vertical);
    layout->setMaximumWidth(width);
    int* leftMargin = new int;
    int* topMargin = new int;
    int* rightMargin = new int;
    int* bottomMargin = new int;
    setLayout(layout);

    //PROJECT
    project = new SideBarPixmap( ":images/resources/project.png", ":images/resources/projectHighlighted.png", width, this );
    layout->addItem(project);
    connect( project, SIGNAL(clicked()), this, SLOT(toggleProjectMenuVisibility()) );

    //SEARCH
    search = new SideBarPixmap( ":images/resources/search.png", ":images/resources/searchHighlighted.png", width, this );
    layout->addItem(search);

    //SORT
    sort = new SideBarPixmap( ":images/resources/sort.png", ":images/resources/sortHighlighted.png", width, this );
    layout->addItem(sort);
    connect( sort, SIGNAL(clicked()), this, SLOT(toggleSortMenuVisibility()) );

    //COLOR
    color = new SideBarPixmap( ":images/resources/color.png", ":images/resources/colorHighlighted.png", width, this );
    layout->addItem(color);

//    //FILTER
//    filter = new SideBarPixmap( ":images/resources/filter.png", ":images/resources/filterHighlighted.png", width, this );
//    layout->addItem(filter);

//    //SEQUENCE
//    sequence = new SideBarPixmap( ":images/resources/sequence.png", ":images/resources/sequenceHighlighted.png", width, this );
//    layout->addItem(sequence);

//    //LINK
//    link = new SideBarPixmap( ":images/resources/link.png", ":images/resources/linkHighlighted.png", width, this );
//    layout->addItem(link);



    //CONTIG HEIGHT
    //----LAYOUT
    contigHeightLayout = new QGraphicsLinearLayout;
    contigHeightLayout->setOrientation(Qt::Horizontal);
    contigHeightLayout->setMaximumWidth(width);
    layout->addItem(contigHeightLayout);

    //----LABEL AND PIXMAP
    contigHeightSideBarPixmap = new SideBarPixmap(":images/resources/contigHeight.png", ":images/resources/contigHeight.png", width*.75, this);
    contigHeightLayout->addItem(contigHeightSideBarPixmap);
    contigHeightLayout->setItemSpacing(0, 0.0);

    contigHeightSlider = new QSlider;
    contigHeightSlider->setMaximumHeight(contigHeightSideBarPixmap->height() );//65.0);//TO DO- won't resize well... =(
    contigHeightSlider->setStyleSheet( "QSlider { background-color : transparent; }");
    contigHeightSlider->setMinimum(50);
    contigHeightSlider->setMaximum(300);
    contigHeightSlider->setValue(100);
    connect(contigHeightSlider, SIGNAL(sliderMoved(int)), this, SIGNAL(heightChangeSignal(int)) );
    contigHeightSliderProxy = new QGraphicsProxyWidget(this);
    contigHeightSliderProxy->setWidget(contigHeightSlider);
    contigHeightLayout->addItem(contigHeightSliderProxy);
    contigHeightLayout->setItemSpacing(1, 0.0);

//ZOOM
    //ZOOM
    //-----PIXMAP
//    zoomSliderPixmap = new SideBarPixmap(":images/resources/zoom.png", ":images/resources/zoom.png", width, this );
//    layout->addItem(zoomSliderPixmap);

//    layout->addItem();

//    zoomLabel = new QLabel;
//    zoomLabel->setText("Zoom");
//    zoomLabelProxy = new QGraphicsProxyWidget(this);
//    zoomLabelProxy->setWidget(zoomLabel);
//    layout->addItem(zoomLabelProxy);

    //ZOOM
    zoomSlider = new QSlider;
    zoomSlider->setOrientation(Qt::Horizontal);
    float ntPerPixel = parent->getNtPerPixel();
    zoomSlider->setValue(100);//ntPerPixel);
    zoomSlider->setMinimum(10);//ntPerPixel*.25);
    zoomSlider->setMaximum(300);//ntPerPixel*4.0);
    zoomSlider->setInvertedControls(true);
    zoomSlider->setInvertedAppearance(true);
    zoomSlider->setTickInterval(10);
    //zoomSlider->setTickPosition(QSlider::TicksBothSides);
    connect(zoomSlider, SIGNAL(sliderMoved(int)), this, SIGNAL(zoomChangeSignal(int)) );

    //zoomSlider->setMaximumHeight(50.0);
    zoomSlider->setStyleSheet( "QSlider { background-color : transparent; }");
    zoomSliderProxy = new QGraphicsProxyWidget(this);
    zoomSliderProxy->setWidget(zoomSlider);
    layout->addItem(zoomSliderProxy);


//    //ZOOM
//    zoomInButton = new QPushButton;
//    zoomInButton->setText("+");
//    zoomInButtonProxy = new QGraphicsProxyWidget(this);
//    zoomInButtonProxy->setWidget(zoomInButton);
//    //layout->addItem(zoomInButtonProxy);

//    zoomOutButton = new QPushButton;
//    zoomOutButton->setText("-");
////    zoomOutButton->setStyleSheet("background-color: black; color: white");
//    zoomOutButtonProxy = new QGraphicsProxyWidget(this);
//    zoomOutButtonProxy->setWidget(zoomOutButton);

//    zoomButtonLayout = new QGraphicsLinearLayout;
//    zoomButtonLayout->setOrientation(Qt::Horizontal);
//    zoomButtonLayout->setMaximumWidth(width);

//    zoomButtonLayout->addItem(zoomInButtonProxy);
//    zoomButtonLayout->addItem(zoomOutButtonProxy);

//    layout->addItem(zoomButtonLayout);

    //ZOOM
    //contigHeightSideBarPixmap = new SideBarPixmap(":images/resources/contigHeight.png", ":images/resources/contigHeight.png", width*.75, this);
//    zoom = new SideBarPixmap(":images/resources/zoom.png", ":images/resources/zoomHighlighted.png", width*.75, this);//may not scale
//    layout->addItem(zoom);

    //PROJECT MENU
    projectMenu = new ProjectMenu(this);
    projectMenu->setPos( project->pos().x() + project->width, 0.0);
    projectMenu->setVisible(false);
    projectMenu->setWidth(300.0);
    projectMenu->setHeight(300.0);
    projectMenu->setZValue(1000.0);


    //SORT MENU
    sortMenu = new SortMenu(this);
    sortMenu->setPos( sort->pos().x() + sort->width, project->height()+search->height()+layout->spacing()*3.0 );//200.0 );//sort->yPos() ); TO DO- shouldn't be manually set
    sortMenu->setWidth( 300.0 );
    sortMenu->setHeight( 300.0 );
    sortMenu->setVisible(false);
    sortMenu->setZValue(1000.0);

}

void GeneRegionViewerMenuSideBar::toggleSortMenuVisibility( )
{
    sortMenu->setVisible( !sortMenu->isVisible() );
}

void GeneRegionViewerMenuSideBar::toggleProjectMenuVisibility()
{
    projectMenu->setVisible( !projectMenu->isVisible() );
}


QRectF GeneRegionViewerMenuSideBar::boundingRect() const
{
    return QRectF( 0, 0, width, height);
}

QPainterPath GeneRegionViewerMenuSideBar::shape() const
{
    QPainterPath path;
    path.addRect(boundingRect());
    return path;
}


void GeneRegionViewerMenuSideBar::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
    QColor backgroundColor;
    backgroundColor.setRgb(0.0, 0.0, 0.0, 255.0);//60.0, 60.0, 60.0, 255.0);
    painter->setBrush(backgroundColor);
    painter->setPen( Qt::NoPen );
    QRectF bRect = boundingRect();
    bRect.adjust(0, 1, 0, -1);
    painter->drawRect(bRect);

}

void GeneRegionViewerMenuSideBar::setWidth(float newWidth)
{
    width = newWidth;
//    progressLabel->setMaximumWidth(width*.80);
//    progressLabelProxy->setMaximumWidth(width*.80);
//    exportButton->setMaximumWidth(width*.80);
//    exportProxyButton->setMaximumWidth(width*.80);
//    sortByComboBox->setMaximumWidth(width*.80);
//    sortByComboBoxProxy->setMaximumWidth(width*.80);
//    sortButton->setMaximumWidth(width*.80);
//    layout->setMaximumWidth(width);
}

void GeneRegionViewerMenuSideBar::setHeight(float newHeight)
{
    height = newHeight;
}
